var urlOrderDetails = server_host + '/mainview/showOrderDetail.do';
var urlGetUserinfo = server_host + '/userweb/getUserInfo.do';
var urlConfirmOrder = server_host + '/userweb/order/ktv/order.do';
var urlSpecialPay = server_host + '/uweb/orderCenter/pay/localSpecialPay.do';
// 红包列表接口
var urlCouponList = server_host + '/uweb/coupon/order/userCouponList.do';

var shopId = getParameterByName('shopId');
var shopTablePriceId = getParameterByName('shopTablePriceId');
var orderDate = getParameterByName('orderDate');
var time = getParameterByName('time');
var themeName = decodeURIComponent(getParameterByName('themeName'));
var tableId = decodeURIComponent(getParameterByName('tableId'));
var styleId = decodeURIComponent(getParameterByName('styleId'));
// 优惠价格
var ticketValue = getParameterByName('ticketValue');
var cash = getParameterByName('cash');
var userCouponId = getParameterByName('userCouponId');

var appModule = angular.module('CheerHiWx', []);
appModule.controller('ConfirmOrderCtrl', ['$scope', '$http', function($scope, $http) {

	$scope.orderDate = orderDate;

	$scope.themeName = themeName;

	$scope.userMsg = '';

	$scope.ticketValue = ticketValue;
	$scope.cash = cash;
	// $scope.newPrice = parseFloat(cash-ticketValue);

	$scope.userCouponId = userCouponId?userCouponId:0;

	$scope.loadList = function() {
		$http.get(urlOrderDetails + '?shopId=' + shopId + '&shopTablePriceId=' + shopTablePriceId + '&orderDate=' + orderDate + '&time=' + time).success(function(data, status, headers, config) {
			console.log(data);
			$scope.orderDetails = data;
		});
	};

	$scope.loadUserinfo = function() {
		$http.get(urlGetUserinfo).success(function(data, status, headers, config) {
			console.log(data);
			$scope.userInfo = data;
		});
	}

	// 登录并继续
	$scope.confirmOrderWithLogin = function() {
		var params = btoa(encodeURIComponent(location.search));
		location.href = 'login.html?params=' + params + '&fr=order';
	}

	// 是否有红包
	$scope.loadCouponList = function(){
		$http.get(urlCouponList + '?shopId=' + shopId + '&cash=' + cash).success(function(data, status, headers, config) {
			console.log(data);
			$scope.CouponList = data.data;
		});
	}

	// 跳到支付页面(正确流程) 确认预订
	$scope.payType = 1;
	$scope.confirmOrder = function() {
		if($scope.listType == 0){
			$scope.payType = 1;
		}else{
			$scope.payType = 2;
		}

		$http.post(urlConfirmOrder + '?shopId=' + shopId + '&shopTablePriceId=' + shopTablePriceId + '&date=' + orderDate + '&time=' + $scope.orderDetails.timeDetail.time + '&endTime=' + $scope.orderDetails.timeDetail.endTime + '&typeCode=' + $scope.orderDetails.typeCode + '&userMsg=' + $scope.userMsg + '&userPhone=' + $scope.userInfo.data.phone + '&tableId=' + tableId + '&styleId=' + styleId + '&payType=' + $scope.payType + '&userCouponId=' + $scope.userCouponId).success(function(data, status, headers, config) {
			console.log(data);

			$scope.newOrderNum = data.data.orderNum;
			// console.log($scope.newOrderNum);

			if (data.isUserLogin) {
				if (data.data.isSuccess) {

					// 价格小于0走下面接口
					if(($scope.cash - $scope.ticketValue) <= 0){
	
						$http.post(urlSpecialPay + '?orderNum=' + $scope.newOrderNum).success(function(data, status, headers, config){
							console.log(data);
							if(data.code == 0){
								location.href = 'ok-pay.html?orderNum=' + $scope.newOrderNum;
							}else{
								// 如果我计算错误，走正常流程！一般不可能发生！艹
								if($scope.payType == 2){
									location.href = 'ok-order.html?orderNum=' + $scope.newOrderNum;
								}else{
									location.href = 'pay-way.html?orderNum=' + $scope.newOrderNum + '&ticketValue=' + $scope.ticketValue;
								}
							}
						})

					}else{		

						if($scope.payType == 2){
							location.href = 'ok-order.html?orderNum=' + data.data.orderNum;
						}else{
							location.href = 'pay-way.html?orderNum=' + data.data.orderNum + '&ticketValue=' + $scope.ticketValue;
						}
					}

				} else {
					alert(data.data.msg);
				}
			} else {
				$scope.confirmOrderWithLogin();
			}



		});
	}

	// 选择支付方式
	$scope.listType = 0;	/*默认状态在线支付*/
	$scope.switchType = function(typeIndex){
		$scope.listType = typeIndex;
		// console.log($scope.listType);
	}

	// 去选择红包
	$scope.GetCoupon = function(){
		location.href = 'use-coupon.html' + '?shopId=' + shopId + '&cash=' + cash + '&styleId=' + styleId + '&tableId=' + tableId + '&orderDate=' + orderDate + '&time=' + time + '&shopTablePriceId=' + shopTablePriceId + '&themeName=' + themeName;
	}

	$scope.asteriskPhoneNum = asteriskPhoneNum;

	$scope.loadList();
	$scope.loadUserinfo();
	$scope.loadCouponList();
}]);